package com.jiegui.wx.service.impl;

import java.util.List;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiegui.common.model.WxUserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.jiegui.wx.mapper.WechatUserMapper;
import com.jiegui.wx.domain.WechatUser;
import com.jiegui.wx.service.IWechatUserService;

/**
 * 微信用户Service业务层处理
 * 
 * @author wuqicheng
 * @date 2021-12-09
 */
@Service
public class WechatUserServiceImpl extends ServiceImpl<WechatUserMapper, WechatUser> implements IWechatUserService
{
    @Resource
    private WechatUserMapper wechatUserMapper;

    /**
     * 查询微信用户
     * 
     * @param id 微信用户主键
     * @return 微信用户
     */
    @Override
    public WechatUser selectWechatUserById(Long id)
    {
        return wechatUserMapper.selectById(id);
    }

    /**
     * 查询微信用户列表
     * 
     * @param wechatUser 微信用户
     * @return 微信用户
     */
    @Override
    public List<WechatUser> selectWechatUserList(WechatUser wechatUser)
    {
        return wechatUserMapper.selectWechatUserList(wechatUser);
    }

    /**
     * 新增微信用户
     * 
     * @param wechatUser 微信用户
     * @return 结果
     */
    @Override
    public int insertWechatUser(WechatUser wechatUser)
    {

        return wechatUserMapper.insert(wechatUser);
    }

    /**
     * 修改微信用户
     * 
     * @param wechatUser 微信用户
     * @return 结果
     */
    @Override
    public int updateWechatUser(WechatUser wechatUser)
    {

        return wechatUserMapper.updateById(wechatUser);
    }

    /**
     * 批量删除微信用户
     * 
     * @param ids 需要删除的微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWechatUserByIds(Long[] ids)
    {
        return wechatUserMapper.delete(new LambdaQueryWrapper<WechatUser>().in(WechatUser::getId,ids));
    }

    /**
     * 删除微信用户信息
     * 
     * @param id 微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWechatUserById(Long id)
    {
        return wechatUserMapper.deleteById(id);
    }

    @Override
    public void refreshUnionidByOpenId(String openId, String unionid) {
        try {
            WechatUser wechatUser = null;
            wechatUser = wechatUserMapper.selectOne(new QueryWrapper<WechatUser>().eq("open_id", openId));
            if (StringUtils.isBlank(wechatUser.getUnionid()) &&
                    StringUtils.isNotBlank(unionid)) {
                wechatUser.setUnionid(unionid);
                wechatUserMapper.updateById(wechatUser);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Override
    public void refreshUnionidByMPOpenId(String openId, WxUserInfo userInfo) {
        try {
            WechatUser wechatUser = null;
            wechatUser = wechatUserMapper.selectOne(new LambdaQueryWrapper<WechatUser>().eq(WechatUser::getOpenId,openId));
            if (StringUtils.isBlank(wechatUser.getUnionid()) &&
                    StringUtils.isNotBlank(userInfo.getUnionid())) {
                wechatUser.setUnionid(userInfo.getUnionid());
                wechatUserMapper.updateById(wechatUser);

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
